package com.xdtech.sys.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.sys.dao.ParamsDao;
import com.xdtech.sys.model.Params;
import com.xdtech.sys.service.ParamsService;
import com.xdtech.sys.vo.ParamsItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-05-31 22:43:28
 * @since 1.0
 * @see
 */
@Service
public class ParamsServiceImpl implements ParamsService {
	private Log log = LogFactory.getLog(ParamsServiceImpl.class);
	@Autowired
	private ParamsDao paramsDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param entity
	 */
	public void save(Params entity) {
		paramsDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<Params> entities) {
		paramsDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param entity
	 */
	public void delete(Params entity) {
		paramsDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		paramsDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param id
	 * @return
	 */
	public Params get(Long id) {
		return paramsDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @return
	 */
	public List<Params> getAll() {
		return paramsDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> paramss = null;
		long rows = 0;
		if (pg!=null) {
			Page<Params> page = paramsDao.findPage(pg,"from Params order by createTime desc", values);
			paramss = BeanUtils.copyListProperties(
					ParamsItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<Params> paramsList = paramsDao.find("from Params order by id desc", values);
			paramss = BeanUtils.copyListProperties(
					ParamsItem.class, paramsList);
			rows = paramss.size();
		}
		results.put("total", rows);
		results.put("rows", paramss);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateParams(ParamsItem item) {
		Params params = null;
		if (item.getId()==null) {
			params = new Params();
		}else {
			params = paramsDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(params, item);
		paramsDao.save(params);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param paramsId
	 * @return
	 */
	public ParamsItem loadParamsItem(Long paramsId) {
		Params params = paramsDao.get(paramsId);
		ParamsItem paramsItem = new ParamsItem();
		BeanUtils.copyProperties(paramsItem, params);
		return paramsItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteParamsInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteParamsInfo(List<Long> paramsIds) {
		for (Long id : paramsIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-31 22:43:28
	 * @modified by
	 * @return
	 */
	@Override
	public List<ParamsItem> loadItems() {
		List<Params> paramss = getAll();
		return ParamsItem.createItems(paramss);
	}

}
